﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
namespace QLNS.Controllers
{
    public class CturController : Controller
    {
        //
        // GET: /Ctur/

        public JsonResult GetPagingGrid(int pagenum, int pagesize, string sortdatafield, string sortorder, int id = 0)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();

            var dbResult = (from mn in context.DSMaNgach
                            join nn in context.DSNhomNgach on mn.IDNhomNgach equals nn.ID
                            //where dt.IDCanBo == id
                            select new { nn.NhomNgach, mn.MaNgach, mn.ID,mn.IDNhomNgach,mn.TenNgach }).ToList();
            //var dbResult = db.Customers.ToList();
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.ID,
                                 customer.IDNhomNgach,
                                 customer.MaNgach,
                                 customer.NhomNgach,
                                 customer.TenNgach

                                
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count,
                Rows = customers
            };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Index()
        {
            List<BangMaNgach> mn = MaNgachModels.GetMaNgachTheoNhomNgach();
            ViewBag.DSMaNgach = mn;
            //CanBo cb = CanBoModels.GetChiTietCanBo(id);
            //ViewBag.ChiTietCanBo = cb;
            return View();
        }
        public ActionResult Add(int id = 0)
        {
            List<BangNhomNgach> dsll = NhomNgachModels.GetDSNhomNgach();
            ViewBag.DSNhomNgach = dsll;
            ViewBag.Id = id;
            return View();
        }
        [HttpPost]
        public ActionResult Add(FormCollection form)
        {
            BangMaNgach mn = new BangMaNgach();
            
            mn.IDNhomNgach = Convert.ToInt32(form["IDNhomNgach"]);
            
            mn.MaNgach = form["MaNgach"];
            mn.TenNgach = form["TenNgach"];
            MaNgachModels.Add(mn);
            //return RedirectToAction("Index");
            return RedirectToAction("Index");
        }

        public ActionResult Update(int id)
        {
            List<BangNhomNgach> nn = NhomNgachModels.GetDSNhomNgach();
            ViewBag.DSNhomNgach = nn;

            BangMaNgach listQHGD = MaNgachModels.GetDSMaNgach(id);
            ViewBag.DSMaNgach = listQHGD;
            return View();
        }
        [HttpPost]
        public ActionResult Update(FormCollection form, int id)
        {
            BangMaNgach mn = MaNgachModels.GetDSMaNgach(id);
            mn.IDNhomNgach = Convert.ToInt32(form["IDNhomNgach"]);
            mn.MaNgach = form["MaNgach"];
            mn.TenNgach = form["TenNgach"];

            MaNgachModels.Update(mn);
            return RedirectToAction("Index");
        }
        public ActionResult Delete(int id)
        {
            BangMaNgach mn = MaNgachModels.GetDSMaNgach(id);
            MaNgachModels.Delete(id);

            return RedirectToAction("Index");
        }

        public int GetConfirmDelete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from cb in context.DSCanBo
                        from mn in context.DSMaNgach
                        where cb.MaNgachCC == mn.MaNgach && mn.ID == id
                        select cb;
            return query.ToList().Count;
        }
    }
}
